<template>
  <!-- 采购单 邀请报价 -->
  <div class="procurementInvitationQuotation">
    <div class="body">
      <div class="g-con">
        <div class="main">
          <div class="title">{{ t('invitationQuotation.t1') }}</div>
          <div class="search flexsc">
            <div class="item">
              <el-input
                v-model="searchData.orderNo"
                :placeholder="t('invitationQuotation.attr1_2')"
              />
            </div>
            <div class="item">
              <el-select-v2
                v-model="searchData.shipId"
                :options="shipOptions"
                :placeholder="t('invitationQuotation.p2')"
                clearable
                filterable
              />
            </div>
            <el-button
              type="primary"
              @click="handleSearch"
            >
              {{ t('invitationQuotation.b1') }}
            </el-button>
            <el-button
              type="primary"
              color="#252628"
              @click="handleReset"
            >
              {{ t('invitationQuotation.b2') }}
            </el-button>
          </div>
          <div class="g-order-list">
            <div
              class="li mt-2"
              v-for="item in listData"
              :key="item.id"
            >
              <div class="attr">
                <div class="item">
                  <span>{{ t('invitationQuotation.attr1_2') }}:</span>
                  <span>{{ item.orderNo }}</span>
                </div>
                <div class="item"></div>
                <div class="item">
                  <span>{{ t('invitationQuotation.attr3') }}:</span>
                  <span>{{ item.createByName }}</span>
                </div>
                <div class="item">
                  <span>{{ t('invitationQuotation.attr4') }}:</span>
                  <span>{{ dateFormat(item.createTime) }}</span>
                </div>

                <!-- 船舶信息 -->
                <template v-if="item.shippingId">
                  <div class="item">
                    <span>{{ t('shipping.l7') }}:</span>
                    <span>{{ item.shipowner }}</span>
                  </div>
                  <div class="item">
                    <span>{{ t('shipping.l3') }}:</span>
                    <span>{{ getDictLabel('ships_type', item.shippingType) }}</span>
                  </div>
                  <div class="item">
                    <span>{{ t('shipping.l8') }}:</span>
                    <span>{{ item.shipManagement }}</span>
                  </div>
                  <div class="item">
                    <span>{{ t('shipping.l12') }}:</span>
                    <span>{{ item.totalLength }}m</span>
                  </div>
                </template>
                <!-- 修船厂 -->
                <template v-else>
                  <div class="item">
                    <span>{{ t('invitationQuotation.attr10') }}:</span>
                    <span>{{ item.shipRepairYardName }}</span>
                  </div>
                  <div class="item">
                    <span>{{ t('invitationQuotation.attr11') }}:</span>
                    <span>{{ item.repairYardCountries }}</span>
                  </div>
                </template>

                <div class="item">
                  <span>{{ t('invitationQuotation.attr12') }}:</span>
                  <span>{{ item.procureDetailCount || 0 }}{{ t('invitationQuotation.attr5_1') }}</span>
                </div>
                <div class="item">
                  <span>{{ t('invitationQuotation.attr13') }}:</span>
                  <span>{{ item.projectLargeName }}</span>
                </div>
                <div class="item">
                  <span>{{ t('invitationQuotation.attr7') }}:</span>
                  <span>{{ dateFormat(item.quotationEndTime) }}</span>
                </div>
                <div class="item">
                  <span>{{ t('invitationQuotation.attr8') }}:</span>
                  <span>{{ item.managerPerson || t('invitationQuotation.attr8_1') }}</span>
                </div>
                <div class="item">
                  <span>{{ t('invitationQuotation.attr9') }}:</span>
                  <span :class="item.orderStatusClass">{{ getDictLabel('order_status', item.orderStatus) }}</span>
                </div>
              </div>

              <!-- 0编辑中，2询价中，5已完成， -->
              <div class="bottom flexsb mt-3">
                <div class="">
                  <el-button
                    type="primary"
                    @click="open(item, 'view')"
                  >
                    {{ t('invitationQuotation.b3') }}
                  </el-button>
                  <!-- isJoinQuote  '1'已参与  '0'已放弃 -->
                  <el-button
                    v-if="item.isJoinQuote === null && isAdmin"
                    type="primary"
                    @click="joinBid(item)"
                  >
                    {{ t('invitationQuotation.b4') }}
                  </el-button>
                  <el-button
                    v-if="item.isJoinQuote === null && isAdmin"
                    type="primary"
                    @click="handleAbandonBid(item)"
                  >
                    {{ t('invitationQuotation.b5') }}
                  </el-button>

                  <!-- 报价单状态 0已拒绝，1中标未确认，2中标已确认，3未提交，4未中标 -->
                  <!-- 未提交 & 已参与 & 未超过报价截止日期 & 报价员 -->
                  <el-button
                    v-if="['3', null].includes(item.quoteOrderStatus) && item.isJoinQuote === '1' && item.isOutweighEndTime === '0' && isQuotaCreateBy(item)"
                    type="primary"
                    @click="onlineQuote(item)"
                  >
                    {{ t('invitationQuotation.b6') }}
                  </el-button>
                  <el-button
                    v-if="item.isJoinQuote === '1' && isAdmin && !item.quotaCreateBy"
                    type="primary"
                    @click="handleCharge(item)"
                  >
                    {{ t('invitationQuotation.b7') }}
                  </el-button>
                  <el-button
                    v-if="isQuotaCreateBy(item)"
                    type="primary"
                    @click="handleAuth(item)"
                  >
                    {{ t('userCenter.b11') }}
                  </el-button>
                </div>
              </div>
            </div>
          </div>

          <div class="flexcc mt-3">
            <el-pagination
              layout="prev, pager, next"
              :hide-on-single-page="total <= page.pageSize"
              :total="total"
              v-model:current-page="page.pageNum"
              :page-size="page.pageSize"
              @current-change="handleCurrentChange"
            >
            </el-pagination>
          </div>
        </div>
      </div>
    </div>

    <!-- 通知 -->
    <Confirm
      v-if="dialogNotification"
      :visible="dialogNotification"
      :title="t('notification')"
      :text="text"
      @close="dialogNotification = false"
      @cancel="dialogNotification = false"
      @confirm="dialogNotification = false"
    />

    <!-- 放弃投标 -->
    <AbandonBid
      v-if="dialogAbandonBid"
      :visible="dialogAbandonBid"
      :orderId="orderId"
      type="2"
      @close="dialogAbandonBid = false"
      @getList="getList"
    />

    <!-- 分配负责人  -->
    <AuthQuoter
      v-if="dialogConfigPerson"
      v-model="dialogConfigPerson"
      :editId="objId"
      type="2"
      @getList="getList"
    />

    <!-- 授权查看人 -->
    <AuthView
      v-if="authViewDialog"
      v-model="authViewDialog"
      :obj-id="objId"
      obj-table="shcw_procure_order"
      @getList="getList"
    />
  </div>
</template>

<script setup>
import { ref, reactive, onBeforeMount, watch, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { queryInvitedOrderApi, onlineQuoteApi, joinBidApi, queryInvitedShipApi } from '@/service/procurement.js'
import { shippingListByEnterpriseIdApi } from '@/service/shipping'
import { useI18n } from 'vue-i18n'
import { dateFormat, openUrl } from '@/utils/index'
import useLogin from '@/stores/login'

import AuthQuoter from '@/components/AuthQuoter.vue' // 分配负责人
import Confirm from '@/components/Confirm.vue' //
import AbandonBid from '../AbandonBid.vue'
import AuthView from '@/components/AuthView.vue'

import useDict from '@/hooks/useDict'
const { dictData, getDictData, getDictLabel } = useDict()

const router = useRouter()
const route = useRoute()
const storeLogin = useLogin()

const { t, locale } = useI18n()
watch(locale, () => {
  init()
})

onBeforeMount(() => {
  init()
})

// 查询
const searchData = ref({})
const shipOptions = ref([])
const getShippingList = () => {
  queryInvitedShipApi().then(({ data }) => {
    shipOptions.value = data.map((r) => {
      return {
        value: r.id,
        label: r.shippingName
      }
    })
  })
}
const handleSearch = () => {
  page.value.pageNum = 1
  if (searchData.value.shipId) {
    searchData.value.shipList = [searchData.value.shipId]
  }
  getList()
}
const handleReset = () => {
  page.value.pageNum = 1
  searchData.value = {}
  getList()
}

// 采购单列表
const listData = ref([])
const page = ref({
  pageNum: 1, // 当前页码
  pageSize: 6 // 每页条数
})
const total = ref(0) // 总数
const getList = () => {
  queryInvitedOrderApi(page.value, searchData.value).then(({ data: rows, total: t }) => {
    rows.forEach((item) => {
      //  0编辑中，2询价中，5已完成
      const colorObj = {
        0: 'blue',
        2: 'yellow',
        5: 'red'
      }
      item.orderStatusClass = colorObj[item.orderStatus]
    })
    listData.value = rows
    total.value = t
  })
}
// 分页
const handleCurrentChange = (val) => {
  getList()
}

// 跳转
const open = (item, type) => {
  if (type === 'view') {
    openUrl('/user/procurementDetail', { id: item.id })
  }
}

// 参与投标
const dialogNotification = ref(false)
const text = ref('')
const joinBid = ({ quotationEndTime, id: orderId }) => {
  joinBidApi({ orderId }).then(() => {
    dialogNotification.value = true
    const obj = {
      'zh-CN': `已确认参与投标，请分配负责人，并于${dateFormat(quotationEndTime)}之前完成报价并提交`,
      en: `Participation in the bid has been confirmed, please assign the person in charge, and complete the bid and submit it before ${dateFormat(quotationEndTime)}`
    }
    text.value = obj[locale.value]
    getList()
  })
}

// 放弃投标
const dialogAbandonBid = ref(false)
const orderId = ref('')
const handleAbandonBid = ({ id }) => {
  dialogAbandonBid.value = true
  orderId.value = id
}

// 在线报价
const onlineQuote = (item) => {
  onlineQuoteApi({
    orderId: item.id
  }).then(({ data }) => {
    openUrl('/user/procurement/onlineQuotation', { id: data, quotaCreateBy: item.quotaCreateBy })
  })
}

// 分配负责人
const objId = ref(null)
const dialogConfigPerson = ref(false)
const handleCharge = ({ id }) => {
  dialogConfigPerson.value = true
  objId.value = id
}

// 是否是管理员
const isAdmin = computed(() => {
  return storeLogin?.userInfo?.isAdmin === '1'
})

// 是否是报价员
const isQuotaCreateBy = (item) => {
  return storeLogin?.userInfo?.id === item.quotaCreateBy
}

// 授权查看人
const authViewDialog = ref(false)
const handleAuth = ({ id }) => {
  authViewDialog.value = true
  objId.value = id
}

//
const init = async () => {
  getShippingList()
  getDictData('ships_type')
  getDictData('order_status')
  getList()
}
</script>

<style scoped lang="scss">
.procurementInvitationQuotation {
  .el-button {
    min-width: 140px;
  }
  .body {
    padding: 30px 0 70px;
    background: #f5f5f5;
    margin-bottom: -76px;

    .main {
      background: #fff;
      padding: 25px 35px;
      .title {
        border-bottom: 2px solid #212121;
        padding-bottom: 20px;
        text-transform: uppercase;
        font-weight: bold;
        font-size: 20px;
        color: var(--mainColor);
      }
      .search {
        margin-top: 25px;
        .item {
          width: 200px;
          margin-right: 20px;
        }
      }
    }
  }
}
</style>
